import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:largefriends/res/lf_color.dart';
import 'package:largefriends/res/lf_double_ext.dart';

import '../config/lf_type_config.dart';
import '../entity/entities/type_config_list_entity.dart';
import '../res/lf_fonts.dart';
import '../res/lf_text_style.dart';
import '../res/strings/lf_strings.dart';
import '../utils/binary_util.dart';
import '../utils/lf_dialog_utils.dart';
import '../utils/lf_scroll_behavior.dart';

showLfMultiPickerSheet({
  required LfConfigTypeEnum configTypeEnum,
  required int value,
  required Function(int result) result,
  Function()? cancel,
  bool needPass = true,
  bool needPlural = false,
  bool isDismissible = true,
  bool mustSelected = false,
  String? title,
}) {
  RxInt selectValue = value.obs;
  var configs = TypeConfig().getTypeConfigListEntity(configTypeEnum);
  List<TypeConfigEntity> temp = List.empty(growable: true);
  temp.addAll(configs.data ?? []);
  if (needPass) {
    temp.insert(0, TypeConfigEntity(key: 0, value: strNoPreference.tr));
  }
  Get.bottomSheet(
      WillPopScope(
        child: Container(
          height: ScreenUtil().screenHeight * 2 / 3,
          padding: EdgeInsets.symmetric(horizontal: 16.w),
          decoration: BoxDecoration(
              color: color000000,
              borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(14.r),
                  topRight: Radius.circular(14.r))),
          child: Column(
            children: [
              Row(
                children: [
                  InkWell(
                    child: Padding(
                      padding:
                          EdgeInsets.only(top: 10.h, bottom: 10.h, right: 10.w),
                      child: Text(
                        strCancel.tr,
                        style: lfTextStyle(sp: 18.sp, fontFamily: fontMedium),
                      ),
                    ),
                    onTap: () {
                      hideSheet();
                      cancel?.call();
                    },
                  ),
                  10.w.sbw,
                  Expanded(
                    child: Text(
                      title ?? "",
                      textAlign: TextAlign.center,
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style: lfTextStyle(sp: 18.sp, fontFamily: fontBold),
                    ),
                  ),
                  10.w.sbw,
                  mustSelected
                      ? Obx(() {
                          bool isEnable = selectValue.value > 0;
                          return InkWell(
                            child: Padding(
                              padding: EdgeInsets.only(
                                  top: 10.h, bottom: 10.h, left: 10.w),
                              child: Text(
                                strDone.tr,
                                style: lfTextStyle(
                                    sp: 18.sp,
                                    fontFamily: fontMedium,
                                    color: isEnable
                                        ? colorFF007F
                                        :Colors.transparent),
                              ),
                            ),
                            onTap: () {
                              if (isEnable == false) {
                                return;
                              }
                              hideSheet();
                              result.call(selectValue.value);
                            },
                          );
                        })
                      : InkWell(
                          child: Padding(
                            padding: EdgeInsets.only(
                                top: 10.h, bottom: 10.h, left: 10.w),
                            child: Text(
                              strDone.tr,
                              style: lfTextStyle(
                                  sp: 18.sp,
                                  fontFamily: fontMedium,
                                  color: color210215),
                            ),
                          ),
                          onTap: () {
                            hideSheet();
                            result.call(selectValue.value);
                          },
                        ),
                ],
              ),
              Container(
                height: 2.h,
                width: ScreenUtil().screenWidth,
                color: color8F8F8F,
              ),
              10.h.sbh,
              Container(
                  decoration: BoxDecoration(
                      color: color8F8F8F,
                      borderRadius: 14.r.radius),
                  padding: EdgeInsets.symmetric(vertical: 10.h),
                  alignment: Alignment.center,
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text(
                        strYouCanSelectMultipleOptions.tr,
                        style: lfTextStyle(fontFamily: fontMedium, sp: 14.sp),
                      )
                    ],
                  )),
              10.h.sbh,
              Expanded(
                  child: ScrollConfiguration(
                behavior: LfScrollBehavior(),
                child: SingleChildScrollView(
                  child: Column(
                      children: temp
                          .map((e) => Obx(() {
                                var text = e.value;
                                if (configTypeEnum == LfConfigTypeEnum.gender &&
                                    needPlural) {
                                  if (e.key == 1) {
                                    text = strWomen.tr;
                                  }
                                  if (e.key == 2) {
                                    text = strMen.tr;
                                  }
                                }
                                bool isSelected =
                                    containKey(e.key, selectValue.value);
                                if (e.key == 0 && selectValue.value == 0) {
                                  isSelected = true;
                                }
                                return InkWell(
                                  child: Padding(
                                    padding:
                                        EdgeInsets.symmetric(vertical: 6.h),
                                    child: Row(
                                      children: [
                                        Expanded(
                                            child: Text(
                                          text,
                                          style: lfTextStyle(
                                              sp: 16.sp,
                                              fontFamily: fontMedium,
                                              color: isSelected
                                                  ? colorFF007F
                                                  : colorFEFEFE),
                                        )),
                                        10.w.sbw,
                                        Opacity(
                                          opacity: isSelected ? 1 : 0,
                                          child: Icon(
                                            Icons.check_circle,
                                            color: colorFF007F,
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                  onTap: () {
                                    if (e.key == 0) {
                                      selectValue.value = 0;
                                    } else if (isSelected) {
                                      selectValue.value =
                                          selectValue.value - e.key;
                                    } else {
                                      selectValue.value =
                                          selectValue.value + e.key;
                                    }
                                  },
                                );
                              }))
                          .toList()),
                ),
              ))
            ],
          ),
        ),
        onWillPop: () async {
          return isDismissible;
        },
      ),
      enableDrag: isDismissible,
      isDismissible: isDismissible);
}
